﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SSP;

namespace SSPDemo
{


    class PlayableLevel: SSPScene
    {
        SSPSprite ground;
        SSPCamera camera;
        SSPScrollingBackground background;
        SSPEnemyManager enemyManager;
        Hero player;
        int LevelLength;
        PlayerAction previousAction;
        
        public PlayableLevel()
        {
            player = new Hero();
            isInputEnabled = true;
        }

        public PlayableLevel(SSPLevelDefinition lvl)
        {
            ground = new SSPSprite("ground");
            LevelLength = lvl.length;
            background = new SSPScrollingBackground(lvl);
            enemyManager = new SSPEnemyManager(lvl);
            camera = new SSPCamera(0, LevelLength, background);
            player = new Hero();

            ground.absolutePosition = new Vector2(640, 710);
            camera.position = new Vector2(640, 360);
            player.position = new Vector2(423, 700);
            this.addChild(ground);
            this.addChild(background);
            this.addChild(player);
            this.addChild(enemyManager);
            player.vertVel = -20;
            isInputEnabled = true;
            SSPAudio.GetInstance.StartBGM();
        }

        public override void update() //game logic
        {
            //Console.WriteLine("PlayableLevel Update()");
            if (camera.position.X > 14800)
            {
                ground.visible = false;
            }
            else
            {
                ground.visible = true;
            }
            if (camera.position.X > LevelLength-100)
            {
                SSPDirector.Instance.replaceScene(new ClosingMovie());
            }
           
            camera.update();
            background.update();
            player.update();
            enemyManager.update();
            enemyManager.cameraPosition = camera.position;
            enemyManager.heroXPosition = player.position.X;
            this.gameLogic();
        }

        public void gameLogic()
        {
            foreach (SSPCharacter enemy in enemyManager.enemyArray)
            {
                if ((enemy.position.X - player.position.X) < player.range)
                {
                    if (player.isAttacking)
                    {
                        enemy.health -= player.damage;
                    }
                }
                if ((enemy.position.X - player.position.X) < enemy.range)
                {
                    if (enemy.isAttacking)
                    {
                        player.health -= enemy.damage;
                    }
                }
            }
        }

        public override void DoPlayerAction(PlayerAction action)
        {
            switch (action)
            {
                case PlayerAction.MoveRight:
                {
                    //Console.WriteLine("MoveRight");
                    player.MoveRight();
                    if (camera.position.X < LevelLength)
                    {
                        camera.position.X += 5;
                    }
                    player.isAttacking = false;
                    break;
                }
                case PlayerAction.MoveLeft:
                {
                    //Console.WriteLine("MoveLeft");
                    player.MoveLeft();
                    if (camera.position.X > 645)
                    {
                        camera.position.X -= 5;
                    }
                    player.isAttacking = false;
                    break;
                }
                case PlayerAction.Jump:
                {
                    //Console.WriteLine("Jump");
                    player.Jump();
                    player.isAttacking = false;
                    break;
                }
                case PlayerAction.Attack:
                {
                    //Console.WriteLine("Attack");
                    player.Attack();
                    player.isAttacking = true;
                      
                    break;
                }
                default:
                {
                    player.Idle();
                    player.isAttacking = false;
                    break;
                }
            }
            previousAction = action;
        }
    }
}
